#自动拾取脚本，可以自动拾取地图上的道具
#此脚本放于末尾，Main之上
#部分移植自哼唧往生

ZiDongShiQuSW=74#X号开关开启后生效
ZiDongShiQuVA=396#自动拾取使用变量
ZiDongShiQu_TSSX=[19,44,48,95,125,141,142,179,195,211,214,229]#DEBUFF属性
ZiDongShiQuSPEED=7#拾取动画速度

ZiDongShiQu=[]#白名单

ZiDongShiQu.push("红宝石")
ZiDongShiQu.push("蓝宝石")
ZiDongShiQu.push("黄宝石")
#ZiDongShiQu.push("血瓶")
#ZiDongShiQu.push("魔瓶")
ZiDongShiQu.push("生命上限")
ZiDongShiQu.push("黄钥匙")
ZiDongShiQu.push("蓝钥匙")
ZiDongShiQu.push("红钥匙")
ZiDongShiQu.push("绿钥匙")

#ZiDongShiQu.push("物品")
ZiDongShiQulc=[]#特殊楼层禁用自动拾取

class Scene_Map
  alias zdsq_map_update update
  alias zdsq_transfer_player transfer_player 
  def update
    if $game_variables[397]==0
      $game_variables[397]=ZiDongShiQu
    end
    if $game_variables[ZiDongShiQuVA]!=0 &&
      $game_variables[ZiDongShiQuVA]!=[]
      for zdsq in $game_variables[ZiDongShiQuVA]
        if $game_map.map_id != zdsq[0]
          $game_variables[ZiDongShiQuVA].delete(zdsq)
          if $game_self_switches[[zdsq[0],zdsq[1],"ZDSQ"]]
            $game_self_switches[[zdsq[0],zdsq[1],"A"]]=true
            $game_self_switches.delete([zdsq[0],zdsq[1],"ZDSQ"])
          end
          next
        end
        e=$game_map.events[zdsq[1]]
        p=$game_player
        if e != nil && e.real_xy(true)==p.real_xy 
          if $game_self_switches[[zdsq[0],zdsq[1],"ZDSQ"]]
            $game_variables[ZiDongShiQuVA].delete(zdsq)
            $game_self_switches[[zdsq[0],zdsq[1],"A"]]=true
            $game_self_switches.delete([zdsq[0],zdsq[1],"ZDSQ"])
          end
        end
      end
    end
#------------------------------------------------------------------------------
    zdsq_map_update
#------------------------------------------------------------------------------
    return if ZiDongShiQulc.include?($game_map.map_id)
    return if $game_system.map_interpreter.running?
    return if $DEBUG and Input.press?(Input::CTRL)
    return if $game_switches[72]
    if $game_actors[$game_variables[1]+1].check_buff(6) || $game_actors[$game_variables[1]+1].check_buff(7)
      return
    end #隐身时
    if $game_switches[68] && $game_switches[67]#$game_variables[281]!=0 && $game_variables[281][$game_map.map_id] !=nil
      return
    end #至死方休
    for i in $game_variables[242]#冰结
      ev=$game_map.events[i]
      xx=(ev.x-$game_player.x).abs
      yy=(ev.y-$game_player.y).abs
      return if xx<=2 and yy<=2
    end
    $game_variables[ZiDongShiQuVA]=[] if $game_variables[ZiDongShiQuVA]==0
    if $game_variables[ZiDongShiQuVA]!=[] 
      for zzzz in $game_variables[ZiDongShiQuVA]
        if $game_map.map_id != zzzz[0]
          $game_variables[ZiDongShiQuVA].delete(zzzz)
          if $game_self_switches[[zzzz[0],zzzz[1],"ZDSQ"]]
            $game_self_switches[[zzzz[0],zzzz[1],"A"]]=true
            $game_self_switches.delete([zzzz[0],zzzz[1],"ZDSQ"])
          end
        end
      end
    end
    return if $game_variables[ZiDongShiQuVA]!=[] 
    p=$game_player
    $zdsqsw=1
    $zdsq=[] if $zdsq==nil
    if $findpath.zdsq_mapdam($game_player.x,$game_player.y)
      eea=1919810
    else
      eea=114514
    end
    mhp=$game_actors[$game_variables[1]+1].maxhp
    hp=$game_actors[$game_variables[1]+1].hp
    msp=$game_actors[$game_variables[1]+1].maxsp
    sp=$game_actors[$game_variables[1]+1].sp
    rate=hp.to_f/mhp
    hptotal=0
    sptotal=0
    maxhpplus=0
    atkplus=0
    denplus=0
    yellowkey=0
    bluekey=0
    redkey=0
    greenkey=0
    @suipian=false
    if $game_switches[77] #开启自动清怪
      esw=true
      if $game_switches[120]
        for i in $game_variables[183]
          esw=false if i>0
        end
      end #开启技能时不清怪
    else
     esw=false
    end
    if $game_switches[ZiDongShiQuSW]
      for i in $game_map.events.values
        next if $zdsq.include?(i.id) 
        next if i.list==nil
        next if $game_self_switches[[$game_map.map_id,i.id,"A"]]   
        next if !$game_switches[ZiDongShiQuSW]
        if esw && i.event.name.include?("怪物") && i.list[0].code==117 &&
          i.list[0].parameters==[1] && hp>0 && i.list.size==2
          eid=i.event.name[6,i.event.name.length-6].to_i
          if (eid>=368 && eid<=372) || eid==652 #排除神符
            $zdsq.push(i.id)
          end
          $zdsq.push(i.id) if eid==0
          for debuff in ZiDongShiQu_TSSX
            $zdsq.push(i.id) if got_p(eid,debuff)
          end
          next if $zdsq.index(i.id)
          if eid != nil && $event_damage[i.id]<=0
            if $game_player.opacity>=240
              if $findpath.find_short_path(i.x,i.y,p.x,p.y,eea)==[]
                $zdsq.push(i.id)
                next
              end
              i.start
              while $game_system.map_interpreter.running?
                break if $game_actors[$game_variables[1]+1].hp<=0
                break if $game_switches[63]
                Graphics.update
                Input.update
                $scene.update
              end
            end
          else
            $zdsq.push(i.id)
          end
          next
        end
        save_mhp=$game_actors[$game_variables[1]+1].maxhp
        $game_variables[397].each do |item| 
          if !i.event.name.include?(item)
            $zdsq.push(i.id)
            next 
          end
          if $findpath.find_short_path(i.x,i.y,p.x,p.y,eea)!=[]
            case item
            when "血瓶"
              next if i.list.size>3
              if mhp*0.7-hp-hptotal>=i.event.name.split(":")[1].to_i
                hptotal+=i.event.name.split(":")[1].to_i
              else
                next
              end
            when "魔法","魔瓶"
              next if i.list.size>3
              if msp*0.7-sp-sptotal>=i.event.name.split(":")[1].to_i
                sptotal+=i.event.name.split(":")[1].to_i
              else
                next
              end
            when "黄钥匙"
              if i.character_name=="Item01-01" && i.direction==8
                next if i.list.size>6
                yellowkey+=3
                if i.event.name.split(":").size>1
                  yellowkey+=i.event.name.split(":")[1].to_i
                end
              else
                next if i.list.size>4
                yellowkey+=1
                if i.event.name.split(":").size>1
                  yellowkey+=i.event.name.split(":")[1].to_i
                end
              end
            when "蓝钥匙"
              if i.character_name=="Item01-01" && i.direction==8
                next if i.list.size>6
                bluekey+=3
                if i.event.name.split(":").size>1
                  bluekey+=i.event.name.split(":")[1].to_i
                end
              else
                next if i.list.size>4
                bluekey+=1
                if i.event.name.split(":").size>1
                  bluekey+=i.event.name.split(":")[1].to_i
                end
              end
            when "红钥匙"
              next if i.list.size>4
              redkey+=1
              if i.event.name.split(":").size>1
                redkey+=i.event.name.split(":")[1].to_i
              end
            when "绿钥匙"
              next if i.list.size>4
              greenkey+=1
              if i.event.name.split(":").size>1
                greenkey+=i.event.name.split(":")[1].to_i
              end
            when "红宝石"
              if i.list.size==2 #宝石碎片
                $game_variables[161]+=1
                if $game_variables[161]==3
                  $game_variables[161]=0
                  atkplus+=1
                end
                @suipian=true
              else #宝石
                next if i.list.size>3
                atkplus+=i.event.name.split(":")[1].to_i
              end
            when "蓝宝石"
              if i.list.size==2 #宝石碎片
                $game_variables[162]+=1
                if $game_variables[162]==3
                  $game_variables[162]=0
                  denplus+=1
                end
                @suipian=true
              else
                next if i.list.size>3
                denplus+=i.event.name.split(":")[1].to_i
              end
            when "黄宝石"
              next if i.list.size>3
              advalue=i.event.name.split(":")[1].to_i
              atkplus+=advalue
              denplus+=advalue
            when "生命上限"
              next if i.list.size>2
              maxhpvalue=i.event.name.split(":")[1].to_i
              maxhpplus+=maxhpvalue
            end
            $game_map.events[i.id].event.name="dead"
            if !$game_variables[41].include?($game_map.map_id)
              $game_variables[41][$game_map.map_id]=[]
            end
            $game_variables[41][$game_map.map_id].insert 0,[i.id,"dead"]
            @erased=false
            #i.start
            i.zdsq_sw
            i.jump(p.x-i.x, p.y-i.y)
            $game_variables[ZiDongShiQuVA].push([$game_map.map_id,i.id])
            $game_self_switches[[$game_map.map_id,i.id,"ZDSQ"]]=true
          else
            $zdsq.push(i.id)
          end
        end
      end
    end
    se1=false
    se2=false
    se3=false
    $game_actors[$game_variables[1]+1].maxhp+=maxhpplus if maxhpplus>0
    if maxhpplus>0
      hptotal+=$game_actors[$game_variables[1]+1].maxhp-save_mhp
      se1=true
    end
    $game_actors[$game_variables[1]+1].hp+=hptotal if hptotal>0
    if hptotal>0
      hp=$game_actors[$game_variables[1]+1].hp
      maxhp=$game_actors[$game_variables[1]+1].maxhp
      $game_actors[$game_variables[1]+1].hprate=hp.to_f/maxhp
      se3=true
    end
    if sptotal>0
      $game_actors[$game_variables[1]+1].sp+=sptotal
      sp=$game_actors[$game_variables[1]+1].sp
      maxsp=$game_actors[$game_variables[1]+1].maxsp
      $game_actors[$game_variables[1]+1].sprate=sp.to_f/maxsp
      se3=true
    end
    if atkplus>0
      $game_actors[$game_variables[1]+1].str+=atkplus
      se1=true
    end
    if denplus>0
      $game_actors[$game_variables[1]+1].dex+=denplus
      se1=true
    end
    if yellowkey>0
      $game_party.gain_item(1,yellowkey)
      se2=true
    end
    if bluekey>0
      $game_party.gain_item(2,bluekey)
      se2=true
    end
    if redkey>0
      $game_party.gain_item(3,redkey)
      se2=true
    end
    if greenkey>0
      $game_party.gain_item(41,greenkey)
      se2=true
    end
    Audio.se_play("Audio/SE/Cansel1",90) if se1
    Audio.se_play("Audio/SE/Cursor1",100) if se2
    Audio.se_play("Audio/SE/cancel1",100) if se3
    $game_switches[56]=true if @suipian==true
    $zdsq=[] if sptotal>0
    $zdsqsw=nil
  end
  def transfer_player
    if $game_variables[ZiDongShiQuVA]!=0
      for zdsq in $game_variables[ZiDongShiQuVA]
        $game_variables[ZiDongShiQuVA].delete(zdsq)
        if $game_self_switches[[zdsq[0],zdsq[1],"ZDSQ"]]
          $game_self_switches[[zdsq[0],zdsq[1],"A"]]=true
          $game_self_switches.delete([zdsq[0],zdsq[1],"ZDSQ"])
        end
      end
    end
    $zdsq=[]#场所移动时刷新
    zdsq_transfer_player
  end
end
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
class Game_Map
  def zdsq_check_event(x, y)
    events = []
    for event in $game_map.events.values
      if event.x == x and event.y == y
        events.push(event.id)
      end
    end
    return events
  end
  #--------------------------------------------------------------------------
  # ● 计算特定方向推移一个图块的 X 坐标（没有循环修正）
  #--------------------------------------------------------------------------
  def x_with_direction(x, d)
    x + (d == 6 ? 1 : d == 4 ? -1 : 0)
  end
  #--------------------------------------------------------------------------
  # ● 计算特定方向推移一个图块的 Y 坐标（没有循环修正）
  #--------------------------------------------------------------------------
  def y_with_direction(y, d)
    y + (d == 2 ? 1 : d == 8 ? -1 : 0)
  end
end
class Window_PlayerDate < Window_Base
  alias zdsq_refresh refresh
  def refresh
    zdsq_refresh
    $zdsq=[]
  end
end
if ZiDongShiQuSW!=0
  class Game_Switches
    def []=(switch_id, value)
      if switch_id <= 5000
        @data[switch_id] = value
      end
      $zdsq=[] if switch_id==ZiDongShiQuSW && value && $zdsqsw==nil#X号开关开启时刷新
    end
  end
end
class Game_SelfSwitches
  def []=(key, value)
    key[2]="ZDSQ" if $game_variables[ZiDongShiQuVA]!=0 && 
    $game_variables[ZiDongShiQuVA].include?([key[0],key[1]])
    @data[key] = value
    if key[2]=="A" && value#独立开关A开启时刷新
      $zdsq=[] if $zdsqsw==nil
      if @data[[key[0],key[1],"ZDSQ:0"]]
        @data.delete([key[0],key[1],"ZDSQ:0"])
        $mapdamage_obj.canvas_visible(key[1]) 
      end
    end
  end
  def delete(key)
    @data.delete(key)
  end
end
class Game_Character
  def zhuilie
    @trg_x=0
    @trg_y=0
  end
  def real_xy(sw=false)
    @move_speed=ZiDongShiQuSPEED if sw
    @x=$game_player.x
    @y=$game_player.y
    return [@real_x,@real_y]
  end
  def zdsq_sw
    @direction_fix=true
    @through=true
    @walk_anime=false
    @move_speed=ZiDongShiQuSPEED
  end
  def get_through
    return @through
  end
end
class Game_Event < Game_Character
  alias zdsq_start start
  def start
    if $game_self_switches[[@map_id,@id,"ZDSQ"]]
      $game_variables[ZiDongShiQuVA].delete([@map_id,@id])
      $game_self_switches[[@map_id,@id,"A"]]=true
      $game_self_switches.delete([@map_id,@id,"ZDSQ"])
    else
      zdsq_start
    end
  end
end
class Interpreter
  alias zdsq_command_106 command_106
  def command_106
    zdsq_command_106 if !$game_self_switches[[@map_id,@event_id,"ZDSQ:0"]]
  end
end
class Map_damage
  def canvas_visible(i)
    @canvas[i].visible=!@canvas[i].visible
  end
end